const gulp = require('gulp')
const less = require('gulp-less')
const postcss = require('gulp-postcss')
const autoprefixer = require('autoprefixer')
const cssnano = require('cssnano')
const rename = require('gulp-rename')

// Less 编译任务
function styles() {
	return gulp
		.src('src/style/index.less')
		.pipe(
			less({
				paths: ['src/style'] // Less 文件查找路径
			})
		)
		.on('error', function (err) {
			console.error('Less 编译错误:', err.message)
			this.emit('end')
		})
		.pipe(
			postcss([
				autoprefixer({
					overrideBrowserslist: ['last 2 versions', '> 1%']
				}),
				cssnano()
			])
		)
		.pipe(rename({ suffix: '.min' }))
		.pipe(gulp.dest('dist'))
}

// 监听文件变化
function watch() {
	gulp.watch('src/style/*.less', styles)
}

// 开发任务
const dev = gulp.series(styles, watch)

// 生产构建任务
const build = gulp.series(styles)

exports.dev = dev
exports.build = build
